<?php

require_once __DIR__.'/vendor/autoload.php';

// 加载环境变量
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

// 创建数据库连接
try {
    $pdo = new PDO(
        "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']};charset=utf8mb4",
        $_ENV['DB_USERNAME'],
        $_ENV['DB_PASSWORD'],
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]
    );

    echo "🎉 HR系统数据修复验证\n";
    echo "================================\n\n";

    // 1. 检查用户
    echo "👤 用户信息:\n";
    $userResult = $pdo->query("SELECT id, username, phone, status FROM users WHERE username = 'admin'");
    $user = $userResult->fetch();
    if ($user) {
        echo "✅ Admin用户存在: {$user['username']} (ID: {$user['id']}, 状态: {$user['status']})\n";
    } else {
        echo "❌ Admin用户不存在\n";
    }

    // 2. 检查角色
    echo "\n🎭 角色信息:\n";
    $rolesResult = $pdo->query("SELECT id, code, name FROM roles ORDER BY id");
    $roles = $rolesResult->fetchAll();
    foreach ($roles as $role) {
        echo "✅ 角色: {$role['name']} ({$role['code']}, ID: {$role['id']})\n";
    }

    // 3. 检查用户角色关联
    echo "\n🔗 用户角色关联:\n";
    $userRoleResult = $pdo->query("
        SELECT u.username, r.name as role_name, ur.start_date, ur.status
        FROM users u
        JOIN user_roles ur ON u.id = ur.user_id
        JOIN roles r ON ur.role_id = r.id
    ");
    $userRoles = $userRoleResult->fetchAll();
    foreach ($userRoles as $ur) {
        echo "✅ {$ur['username']} -> {$ur['role_name']} (开始日期: {$ur['start_date']}, 状态: {$ur['status']})\n";
    }

    // 4. 检查菜单
    echo "\n📋 菜单信息:\n";
    $menusResult = $pdo->query("SELECT code, name, path FROM menus ORDER BY sort_order");
    $menus = $menusResult->fetchAll();
    foreach ($menus as $menu) {
        echo "✅ 菜单: {$menu['name']} ({$menu['code']}) -> {$menu['path']}\n";
    }

    // 5. 检查功能
    echo "\n⚙️ 功能信息:\n";
    $functionsResult = $pdo->query("SELECT code, name, level FROM functions ORDER BY menu_id, sort_order");
    $functions = $functionsResult->fetchAll();
    foreach ($functions as $function) {
        echo "✅ 功能: {$function['name']} ({$function['code']}, 级别: {$function['level']})\n";
    }

    // 6. 检查权限节点
    echo "\n🔐 权限节点:\n";
    $permissionsResult = $pdo->query("SELECT code, name, type FROM permission_nodes ORDER BY type, code");
    $permissions = $permissionsResult->fetchAll();
    foreach ($permissions as $permission) {
        echo "✅ 权限: {$permission['name']} ({$permission['code']}, 类型: {$permission['type']})\n";
    }

    // 7. 检查超级管理员权限
    echo "\n👑 超级管理员权限:\n";
    $superAdminPermissionsResult = $pdo->query("
        SELECT COUNT(*) as count
        FROM role_function_permissions rfp
        JOIN functions f ON rfp.function_id = f.id
        WHERE rfp.role_id = 1
    ");
    $superAdminPermissions = $superAdminPermissionsResult->fetch();
    echo "✅ 超级管理员拥有 {$superAdminPermissions['count']} 个功能权限\n";

    // 8. 统计信息
    echo "\n📊 系统统计:\n";
    $stats = [
        'users' => $pdo->query("SELECT COUNT(*) as count FROM users")->fetch()['count'],
        'roles' => $pdo->query("SELECT COUNT(*) as count FROM roles")->fetch()['count'],
        'menus' => $pdo->query("SELECT COUNT(*) as count FROM menus")->fetch()['count'],
        'functions' => $pdo->query("SELECT COUNT(*) as count FROM functions")->fetch()['count'],
        'permission_nodes' => $pdo->query("SELECT COUNT(*) as count FROM permission_nodes")->fetch()['count'],
        'user_roles' => $pdo->query("SELECT COUNT(*) as count FROM user_roles")->fetch()['count'],
        'function_permissions' => $pdo->query("SELECT COUNT(*) as count FROM function_permissions")->fetch()['count'],
        'role_function_permissions' => $pdo->query("SELECT COUNT(*) as count FROM role_function_permissions")->fetch()['count']
    ];

    foreach ($stats as $table => $count) {
        echo "✅ {$table}: {$count} 条记录\n";
    }

    echo "\n🎯 登录信息:\n";
    echo "用户名: admin\n";
    echo "密码: admin123\n";
    echo "角色: 超级管理员\n";

    echo "\n✅ 数据修复完成！系统可以正常使用了。\n";

} catch (Exception $e) {
    echo "❌ 错误: " . $e->getMessage() . "\n";
}
